package com.duowei.kitchenshow.fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceFragment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import com.duowei.kitchenshow.R;
import com.duowei.kitchenshow.utils.SpUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class PrintSettingFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

    private SharedPreferences mSpf;
    private EditTextPreference mEtPrintIp;

    public PrintSettingFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference_print);
        mSpf = getPreferenceScreen().getSharedPreferences();
        String printIp = mSpf.getString(getString(R.string.pringIp), "");

        mEtPrintIp = (EditTextPreference) findPreference(getString(R.string.pringIp));
        mEtPrintIp.setSummary(printIp);

        mSpf.registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSpf.unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if(key.equals(getString(R.string.pringIp))){
            String printIp = mSpf.getString(key, "");
            mEtPrintIp.setSummary(printIp);
            SpUtils.put(getString(R.string.pringIp),printIp);
        }
    }
}
